package com.icongyou.enterprise.talent_marking;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.talent_marking.entity.dto.MarkRequest;
import com.icongyou.enterprise.talent_marking.entity.vo.StudentFocusMark;
import com.icongyou.enterprise.talent_marking.service.StudentFocusMarkService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class StudentFocusMarkServiceTest {

    @Mock
    private StudentFocusMarkService studentFocusMarkService;

    private String userId;
    private String studentId;
    private MarkRequest markRequest;

    @BeforeEach
    void setUp() {
        userId = "USER001";
        studentId = "STU001";

        markRequest = new MarkRequest();
        markRequest.setUserId(userId);
        markRequest.setStudentId(studentId);
    }

    @Test
    void markStudent_WithValidRequest_ShouldReturnSuccess() {
        // Arrange
        Result<String> expectedResult = Result.success("关注成功");
        when(studentFocusMarkService.markStudent(any(MarkRequest.class))).thenReturn(expectedResult);

        // Act
        Result<String> result = studentFocusMarkService.markStudent(markRequest);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertEquals("关注成功", result.getData());

        verify(studentFocusMarkService).markStudent(markRequest);
    }

    @Test
    void markStudent_WithInvalidRequest_ShouldReturnFailure() {
        // Arrange
        Result<String> expectedResult = Result.error("关注失败");
        when(studentFocusMarkService.markStudent(any(MarkRequest.class))).thenReturn(expectedResult);

        // Act
        Result<String> result = studentFocusMarkService.markStudent(markRequest);

        // Assert
        assertNotNull(result);
        assertEquals(500, result.getCode());
        assertEquals("关注失败", result.getMessage());

        verify(studentFocusMarkService).markStudent(markRequest);
    }

    @Test
    void unmarkStudent_WithValidParameters_ShouldReturnSuccess() {
        // Arrange
        Result<String> expectedResult = Result.success("取消关注成功");
        when(studentFocusMarkService.unmarkStudent(userId, studentId)).thenReturn(expectedResult);

        // Act
        Result<String> result = studentFocusMarkService.unmarkStudent(userId, studentId);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertEquals("取消关注成功", result.getData());

        verify(studentFocusMarkService).unmarkStudent(userId, studentId);
    }

    @Test
    void getFocusStudentsByUser_ShouldReturnStudentList() {
        // Arrange
        List<StudentFocusMark> focusStudents = Arrays.asList(
                new StudentFocusMark(),
                new StudentFocusMark()
        );
        Result<List<StudentFocusMark>> expectedResult = Result.success(focusStudents);
        when(studentFocusMarkService.getFocusStudentsByUser(userId)).thenReturn(expectedResult);

        // Act
        Result<List<StudentFocusMark>> result = studentFocusMarkService.getFocusStudentsByUser(userId);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertEquals(2, result.getData().size());

        verify(studentFocusMarkService).getFocusStudentsByUser(userId);
    }

    @Test
    void isStudentFocusMarked_WhenMarked_ShouldReturnTrue() {
        // Arrange
        Result<Boolean> expectedResult = Result.success(true);
        when(studentFocusMarkService.isStudentFocusMarked(userId, studentId)).thenReturn(expectedResult);

        // Act
        Result<Boolean> result = studentFocusMarkService.isStudentFocusMarked(userId, studentId);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertTrue(result.getData());

        verify(studentFocusMarkService).isStudentFocusMarked(userId, studentId);
    }

    @Test
    void isStudentFocusMarked_WhenNotMarked_ShouldReturnFalse() {
        // Arrange
        Result<Boolean> expectedResult = Result.success(false);
        when(studentFocusMarkService.isStudentFocusMarked(userId, studentId)).thenReturn(expectedResult);

        // Act
        Result<Boolean> result = studentFocusMarkService.isStudentFocusMarked(userId, studentId);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertFalse(result.getData());

        verify(studentFocusMarkService).isStudentFocusMarked(userId, studentId);
    }

    @Test
    void getFocusStudentInfo_ShouldReturnStudentInfo() {
        // Arrange
        List<StudentFocusMark> studentInfo = Arrays.asList(new StudentFocusMark());
        Result<List<StudentFocusMark>> expectedResult = Result.success(studentInfo);
        when(studentFocusMarkService.getFocusStudentInfo(userId)).thenReturn(expectedResult);

        // Act
        Result<List<StudentFocusMark>> result = studentFocusMarkService.getFocusStudentInfo(userId);

        // Assert
        assertNotNull(result);
        assertEquals(200, result.getCode());
        assertEquals(1, result.getData().size());

        verify(studentFocusMarkService).getFocusStudentInfo(userId);
    }
}
